﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OfficeOpenXml;

namespace ServisniList.Rukovatelji
{
    public class ExportStranakaUExcelAdapter
    {
        public static void Export(Core.DataModelDataContext context, string path)
        {
            using (ExcelPackage package = new ExcelPackage())
            {
                #region Meta data

                package.Workbook.Properties.Author = "Pnp doo";
                package.Workbook.Properties.Title = "Servisni list stranke zaglavlje";

                package.Workbook.Worksheets.Add("Servisni list stranke");
                ExcelWorksheet newSheet = package.Workbook.Worksheets[1];
                newSheet.Name = "Servisni list stranke";

                newSheet.Cells.Style.Font.Size = 11;
                newSheet.Cells.Style.Font.Name = "Calibri";

                #endregion

                #region Generiranje podataka
                
                var stranke = context.Strankas.ToList();

                ExcelExportHelpers.postaviHeaderCeliju(newSheet.Cells[1, 1], "Stranka");
                ExcelExportHelpers.postaviHeaderCeliju(newSheet.Cells[1, 2], "Broj mobitela");

                for (int i = 0; i < stranke.Count; i++)
                {
                    ExcelExportHelpers.postavipodatakCeliju(newSheet.Cells[i + 2, 1], stranke[i].Naziv);
                    ExcelExportHelpers.postavipodatakCeliju(newSheet.Cells[i + 2, 2], stranke[i].BrojMobitela);
                }
                
                #endregion

                Byte[] bin = package.GetAsByteArray();
                File.WriteAllBytes(path, bin);
            }
        }
    }
}
